package xerror

// BusinessError 业务错误类型
type BusinessError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func (e *BusinessError) Error() string {
	return e.Message
}

// NewBusinessError 创建业务错误
func NewBusinessError(code int, message string) *BusinessError {
	return &BusinessError{
		Code:    code,
		Message: message,
	}
}

// 常用业务错误码
const (
	CodeInvalidParam = 400
	CodeUnauthorized = 401
	CodeForbidden    = 403
	CodeNotFound     = 404
	CodeConflict     = 409
)

// 通用错误
func New(message string) *BusinessError {
	return NewBusinessError(500, message)
}

// 常用业务错误构造函数
func NewInvalidParamError(message string) *BusinessError {
	return NewBusinessError(CodeInvalidParam, message)
}

func NewUnauthorizedError(message string) *BusinessError {
	return NewBusinessError(CodeUnauthorized, message)
}

func NewForbiddenError(message string) *BusinessError {
	return NewBusinessError(CodeForbidden, message)
}

func NewNotFoundError(message string) *BusinessError {
	return NewBusinessError(CodeNotFound, message)
}

func NewConflictError(message string) *BusinessError {
	return NewBusinessError(CodeConflict, message)
}
